#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "utils.h"
#include "base.h"

#define base_self(obj) cobject_self((obj), CCLASS_NAME(base_s))

static inline void __base_init(base_s *self)
{
	self->base_get_val = NULL;
	self->base_set_val = NULL;
    self->a = 0;
}

base_s *base_new(void)
{
    base_s *self = cobject_new(CCLASS_INFO(base_s));
    if (self) __base_init(self);
    return self;
}

void base_del(base_s **pself)
{
    assert(pself);
    assert(base_type(*pself));

    if (!(*pself)) return;

    __base_init(*pself);
    cobject_del(*pself);
    *pself = NULL;
}

int base_ctor(base_s *self)
{
    assert(self);

    cobject_ctor(self, CCLASS_INFO(base_s));
    __base_init(self);
    return 0;
}

void base_dtor(base_s *self)
{
    assert(self);
    assert(base_type(self));

    __base_init(self);
    cobject_dtor(self);
}

int base_print(cobject obj)
{
    base_s *self = base_self(obj);

    assert(self);

    debug("a=%d\n", self->a);
    return 0;
}

CVIRTUAL int base_get_val(cobject obj)
{
    base_s *self = base_self(obj);

    assert(self);

    // 若为子类对象，执行子类对虚函数的重写
    if (self != obj && self->base_get_val) return self->base_get_val(obj); 
    return self->a;
}

CVIRTUAL int base_set_val(cobject obj, int val)
{
    base_s *self = base_self(obj);

    // 若为子类对象，执行子类对虚函数的重写
    if (self != obj && self->base_set_val) return self->base_set_val(obj, val); 

    self->a = val;
    return 0;
}
